import { getAction, httpAction } from '@api/manage'

export const GfxyOneToOneMixin = {
  components: {

  },
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      startDateOpen:false,
      endDateOpen:false,
      confirmLoading : false,
      model:{
        itemValue: this.itemValue,
        totalScore: 0.0,
        date: this.date,
        orgId:''
      },
      url:{
        getDepUrl:"/common/memberList/queryDepartmentByUserName",
      }
    }
  },
  methods: {
    add(){
      this.edit(this.model)
    },
    edit(record) {

      if (typeof this.editBefore === 'function') this.editBefore(record)
      this.visible = true
      let _model = Object.assign({}, record)
      if(_model.id != undefined){
        getAction(this.url.memberList.list ,{id: _model.id}).then((res)=>{
          if(res.success){
            if(res.result.length>0) {
              _model.teacherId = res.result[0].teacherId;
              _model.orgId = res.result[0].orgId;
              this.model = _model;
            }
          }else{
            this.$message.warning(res.message);
          }
        }).finally(() => {
          console.log("finally")
        })
      }
      if (typeof this.editAfter === 'function') this.editAfter(this.model)
    },
    close() {
      this.visible = false
      this.$emit('close')
    },
    validateError(msg){
      this.$message.error(msg)
    },

    selectedUser(name,id){

      this.queryDep(id);
    },
    queryDep(){
      getAction(this.url.getDepUrl ,{userName: this.model.teacherId}).then((res)=>{
        if(res.success){
          console.log(res.result)
          this.model.orgId = res.result.id;
          this.model.orgId_dictText = res.result.departName;
          console.log(this.model)
        }else{
          this.$message.warning(res.message);
        }
      }).finally(() => {
        console.log("finally")
      })
    },
    handleOk(){

      new Promise((resolve, reject) => {
        // 验证主表表单
        this.$refs.form.validate((valid,obj) => {
          console.log(valid);
          console.log(obj)
          valid ? resolve() : reject(obj)
        })
      }).then(()=>{
        let param = {
          ...this.model,
          memberListList: [{
            ranked: 1,
            teacherId: this.model.teacherId,
            orgId: this.model.orgId,
            score: this.model.totalScore,
            isCustom:0,
            itemValue: this.itemValue,
            date:this.date
          }]
        }
        console.log(param)
        this.request(param)
      }).catch(()=>{
        this.validateError('请输入必填项')
        return;
      })
    },
    request(formData) {
      let url = this.url.add, method = 'post'
      if (this.model.id) {
        url = this.url.edit
        method = 'put'
      }
      this.confirmLoading = true
      console.log(formData);

      httpAction(url, formData, method).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.$emit('ok')
          this.close()
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.confirmLoading = false
      })
    },
  }
}